import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:jtlf/collaboration.dart';
import 'package:jtlf/contacts.dart';
import 'package:jtlf/me.dart';
import 'package:jtlf/message.dart';
import 'package:jtlf/splash.dart';
import 'package:jtlf/workbench.dart';

void main() {
  runApp(const MySplashApp());
}

class MyJtlfApp extends StatelessWidget {
  const MyJtlfApp({super.key});

  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
    ));
    return MaterialApp(
      title: '金町利丰',
      theme: ThemeData(
        primaryColor: Color(0xFF07C160),
        scaffoldBackgroundColor: Colors.white,
      ),
      home: MainHome(),
    );
  }
}

class MainHome extends StatefulWidget {
  const MainHome({super.key});

  @override
  _MainHomeState createState() => _MainHomeState();
}

class _MainHomeState extends State<MainHome> {
  int _currentIndex = 0;
  final List<Widget> _pages = [
    // ChatListPage(),
    MessageListPage(), // 消息页
    CollaborationPage(), // 协作
    // ContactPage(),
    ContactsPage(), // 联系人
    // DiscoverPage(),
    WorkbenchPage(), // 工作台页
    // MePage(),
    ProfilePage() // 我的
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: _buildAppBar(), // 应该是这里设置了公共的appBar 2025-3-15
      body: IndexedStack(
        index: _currentIndex,
        children: _pages,
      ),
      bottomNavigationBar: _buildBottomNavigationBar(),
    );
  }

  // PreferredSizeWidget _buildAppBar() {
  //   return AppBar(
  //     title: Text(_getAppBarTitle()),
  //     elevation: 0,
  //     actions: _currentIndex == 0 ? [
  //       IconButton(icon: Icon(Icons.search), onPressed: () {}),
  //       IconButton(icon: Icon(Icons.add), onPressed: () {}),
  //     ] : null,
  //   );
  // }

  // String _getAppBarTitle() {
  //   switch (_currentIndex) {
  //     case 0: return '消息';
  //     case 1: return '协作';
  //     case 2: return '通讯录';
  //     case 3: return '工作台';
  //     case 4: return '我';
  //     default: return '';
  //   }
  // }

  Widget _buildBottomNavigationBar() {
    return BottomNavigationBar(
      currentIndex: _currentIndex,
      type: BottomNavigationBarType.fixed,
      selectedItemColor: Color(0xFF07C160),
      unselectedItemColor: Colors.grey[600],
      onTap: (index) => setState(() => _currentIndex = index),
      items: [
        BottomNavigationBarItem(
          icon: Icon(Icons.chat_bubble_outline),
          activeIcon: Icon(Icons.chat_bubble),
          label: '消息',
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.shopping_bag_outlined),
          activeIcon: Icon(Icons.shopping_bag),
          label: '协作',
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.contacts_outlined),
          activeIcon: Icon(Icons.contacts),
          label: '通讯录',
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.explore_outlined),
          activeIcon: Icon(Icons.explore),
          label: '工作台',
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.person_outline),
          activeIcon: Icon(Icons.person),
          label: '我',
        ),
      ],
    );
  }
}

// 聊天列表页面
// class ChatListPage extends StatelessWidget {
//   final List<ChatItem> _chatItems = [
//     ChatItem(
//       name: '张三',
//       lastMessage: '你好，最近怎么样？',
//       time: '10:30',
//       unread: 2,
//       avatar: 'https://example.com/avatar1.png',
//     ),
//     // 更多聊天项...
//   ];
//
//   @override
//   Widget build(BuildContext context) {
//     return ListView.builder(
//       itemCount: _chatItems.length,
//       itemBuilder: (context, index) => _buildChatItem(_chatItems[index]),
//     );
//   }
//
//   Widget _buildChatItem(ChatItem item) {
//     return ListTile(
//       leading: CircleAvatar(
//         backgroundImage: NetworkImage(item.avatar),
//         radius: 25,
//       ),
//       title: Row(
//         children: [
//           Expanded(child: Text(item.name)),
//           Text(item.time, style: TextStyle(color: Colors.grey, fontSize: 12)),
//         ],
//       ),
//       subtitle: Row(
//         children: [
//           Expanded(child: Text(item.lastMessage)),
//           if (item.unread > 0)
//             Container(
//               padding: EdgeInsets.all(4),
//               decoration: BoxDecoration(
//                 color: Colors.red,
//                 borderRadius: BorderRadius.circular(10),
//               ),
//               child: Text(
//                 item.unread.toString(),
//                 style: TextStyle(color: Colors.white, fontSize: 12),
//               ),
//             ),
//         ],
//       ),
//       onTap: () {},
//     );
//   }
// }

// 其他页面占位组件
// class ContactPage extends StatelessWidget {
//   @override
//   Widget build(BuildContext context) => Center(child: Text('通讯录页面'));
// }
//
// class DiscoverPage extends StatelessWidget {
//   @override
//   Widget build(BuildContext context) => Center(child: Text('工作台页面'));
// }
//
// class MePage extends StatelessWidget {
//   @override
//   Widget build(BuildContext context) => Center(child: Text('个人页面'));
// }

// 数据模型
class ChatItem {
  final String name;
  final String lastMessage;
  final String time;
  final int unread;
  final String avatar;

  ChatItem({
    required this.name,
    required this.lastMessage,
    required this.time,
    required this.unread,
    required this.avatar,
  });
}
